package controller;

import java.sql.SQLException;
import parser.MySpaceParser;
import util.ExecutionTimeCalculate;
import util.FileDirectoryHandler;

/**
 * This is the main class of parser package. 
 * This application is responsible for parsing the XML documents
 * and insert into MySQL tables.
 *
 */
public class App {

    private final static String BASE_DIR = "C:/Users/ITE/Desktop/";
   // private final static String BASE_DIR = "/Users/amendrashrestha/Desktop/" ;
    private final static String FILE = "TestMySpace";
    private final static String successMessage = "Files successfully parsed";
    private final static String failureMessage = "Error occurred while parsing";
    private final static String startParsing = "****Parsing Started*****";
    private final static String endParsing = "****Completed Parsing*******";
    private static String files[];

    public App() throws SQLException {

        long startTime = System.currentTimeMillis();

        FileDirectoryHandler.rootList(BASE_DIR + FILE);
        int noOfFiles = FileDirectoryHandler.getList().size();
        files = new String[noOfFiles];

        FileDirectoryHandler.list.toArray(files);
            try {
            for (int i = 0; i < files.length; i++) {
                String filename = files[i];
                System.out.println("\nFilename: " + filename);
                System.out.println(startParsing);
                MySpaceParser init = new MySpaceParser();
                init.parseMySpace(filename);
                System.out.println(endParsing);
            }
            System.out.println(successMessage);
            long endTime = System.currentTimeMillis();
            //calculats the execution time for user files
            System.out.println("User Execution Time: " + ExecutionTimeCalculate.returnTime(startTime, endTime) + " min");
        } catch (Error e) {
            System.out.println(e.getMessage());
        }
    }

    public static void main(String[] args) throws SQLException {

        App init = new App();
    }
}
